<?php
/**
 * Free Uch
 *
 * @category   FreeUch
 * @copyright  Copyright 2009 lordharrdlee(http://www.lordharrdlee.cn)
 * @author     lordharrdlee<lordharrdlee@gmail.com>
 * @version    $Id: Base.php 10 2010-01-14 10:31:01Z lordharrdlee $
 */
if (! defined('IN_UCHOME')) {
    exit('Access Denied');
}
/**
 * Action Base
 * @author     lordharrdlee<lordharrdlee@gmail.com>
 */
abstract class Action_Base
{

    /**
     * autoload model 
     * @var bool
     */
    protected $useModel = true;

    /**
     * @var Db_Manager $db
     */
    protected $db;

    /**
     * @var object $view
     */
    protected $view;

    /**
     * @var string $modName
     */
    protected $modName;

    /**
     * @var string $submodName
     */
    protected $submodName = '';

    /**
     * @var Cache_Impl_FcacheImpl $fileCache
     */
    protected $fileCache;

    /**
     * @var Cache_Impl_McacheImpl  $memCache
     */
    protected $memCache;

    /**
     * @var Model_Base $model
     */
    protected $model;

    /**
     * @var array $SGLOBAL
     */
    protected $SGLOBAL;

    /**
     * @var array $space
     */
    protected $space;

    /**
     * construct
     */
    public function __construct()
    {
        global $_SGLOBAL;
        $this->SGLOBAL = $_SGLOBAL;
        $uid = empty($_GET['uid'])?0:intval($_GET['uid']);
        if($uid) {
            $this->space = getspace($uid, 'uid');
        } elseif ($_SGLOBAL['supe_uid']) {
            $this->space = getspace($_SGLOBAL['supe_uid'], 'uid');
        }
        $this->init();
    }

    /**
     * init
     */
    public function init(){}

    /**
     * set modname
     * @param $modName
     */
    public function setMod($modName)
    {
        $this->modName = $modName;
    }

    /**
     * set submodname
     * @param $modName
     */
    public function setSubmod($submodName)
    {
        $this->submodName = $submodName ? $submodName . '/' : '';
    }

    /**
     * check if autoload model
     */
    public function getUseModel()
    {
        return $this->useModel;
    }

    /**
     * get loaded model
     */
    public function getModel()
    {
        return $this->model;
    }

    /**
     * set loaded model
     * @param $model
     */
    public function setModel($model)
    {
        $this->model = $model;
    }

    /**
     * display template
     * 
     * @param string $tpl
     */
    protected function display($tpl)
    {
        global $_SC,$_SGLOBAL,$_SCONFIG,$_TPL,$_SN;
        if($this->view) {
            foreach($this->view as $key => $value) {
                $$key = $value;
            }
        }
        $space = $this->space;
        include template( 'freeuch/templates/' . $this->submodName . $this->modName . '/' . $tpl);
    }

    /**
     * get file cache obj
     *
     * @param string $cacheDir
     * @param int $cacheDate (seconds)
     * @param int $cacheLevel
     * @return Cache_Impl_McacheImpl $this->fileCache
     */
    protected function getFileCache($cacheDir = '', $cacheDate = 3600, $cacheLevel = 2)
    {
        if (empty($cacheDir)) {
            $cacheDir = CACHE_PATH . $this->modName . DIRECTORY_SEPARATOR;
            if (! file_exists($cacheDir)) {
                mkdir($cacheDir, 0777);
                chmod($cacheDir, 777);
            }
        }
        $this->fileCache = Cache_Factory::getFileCache($cacheDir, $cacheDate, $cacheLevel);
        return $this->fileCache;
    }

    /**
     * get Memcache obj
     * 
     * @return Cache_Impl_McacheImpl $this->memCache
     */
    protected function getMemCache()
    {
        $this->memCache = Cache_Factory::getMemCache();
        return $this->memCache;
    }

    protected function redirect($url)
    {
        obclean();
        header("HTTP/1.1 301 Moved Permanently");
	header("Location: $url");
    }

    protected function isLogin()
    {
        global $_SGLOBAL;
        if($_SGLOBAL['supe_uid']) {
            return true;
        }
        return false;
    }
}